// pages/shopAddress/shopAddress.js
import { areaList } from '../../dist/area-data/dist/index.cjs';
const util = require('../../utils/util.js')
const app = getApp();
const cloudUrl = app.globalData.cloudUrl;
import Toast from '../../dist/toast/toast';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    cloudUrl:app.globalData.cloudUrl,
    templateData:[{idx:0,name:"收货人",preTmp:"请输入收货人",pre:"请输入收货人",type:"text",txt:"",disabled:false,inputStyle:""},
                  {idx:1,name:"联系电话",preTmp:"请输入电话号码",pre:"请输入电话号码",type:"number",txt:"",disabled:false,inputStyle:""},
                  {idx:2,name:"所在地区",preTmp:"请输入所在地",pre:"请输入所在地",type:"text",txt:"",disabled:true,inputStyle:""}
    ],
    popAdd:false,
    popArea:false,
    popSwitch:true,
    areaList:areaList,
    areaTxt:"",
    addressList:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    wx.setNavigationBarTitle({
      title: "选择地址",
    })

    this.getUserInfo();
  },
  getUserInfo(){
    util.getAndSetUserInfo(this.onUserBack);
  },
  onUserBack(code){
    console.log("get userinfo result:"+code);
    const app = getApp();
    const list = app.globalData.userInfo.addressList.concat();
    this.setData({addressList:list});
  },
  onRadioClick(e){
    const idx = e.currentTarget.dataset.index;
    const list = this.data.addressList;
    const info = list[idx];
    const curSelect = !info.select;
    if(curSelect){
      for(var i=0; i<list.length; i++){
        list[i].select = false;
      }
    }
    info.select = curSelect;
    this.setData({addressList:this.data.addressList});
  },
  onPopSwitch(e){
    this.setData({popSwitch:e.detail.value});
  },
  onSaveClick(e){
    var datas = this.data.templateData;
    var errData = null;
    for(var i=0; i<datas.length; i++){
      if(!(datas[i].txt)){
        errData = datas[i];
        break;
      }
    }
    if(errData){
      Toast(errData.pre);
    }
    else{
      if(!this.data.areaTxt){
        Toast("请填写详细地址");
      }
      else{
        const app = getApp();
        const _this = this;
        _this.setData({popAdd:false});

        var no = Math.floor(Math.random() * 999999);
        var date = new Date();
        var key = date.getTime() + "" + no;//暂时这样写。
        var address = datas[2].txt + this.data.areaTxt;
        const curData = {
          type:"setAddress",
          cmdType:"push",
          openid:app.globalData.openid,
          key:key,
          name:datas[0].txt,
          phone:datas[1].txt,
          address:address,
          select:_this.data.popSwitch
        }
        //保存地址
        wx.cloud.callFunction({
          name: 'quickstartFunctions',
          data: curData,
          complete: res => {
            console.log('setAddress push: ', res);//
            //_this.getUserInfo();
            
            const select = _this.data.popSwitch;
            const list = _this.data.addressList;
            if(select){
              for(var i=0; i<list.length; i++){
                list[i].select = false;
              }
            }
            list.push(curData);
            _this.setData({popAdd:false,addressList:list});
            Toast("新地址添加成功！");
          }
        })
      }
    }
  },
  onPopClose(e){
    this.setData({popAdd:false});
  },
  onAddBtnClick(e){
    this.setData({popAdd:true});
  },
  onTextArea(e){
    var value = e.detail.value;
    if(value){
      this.setData({areaTxt:value});
    }
  },
  onInput(e){
    const idx = e.currentTarget.dataset.index;
    var value = e.detail.value;
    var datas = this.data.templateData;
    if(value){
      console.log("idx="+idx+",value="+value);
      datas[idx].txt = value;
      datas[idx].pre = "";
      this.setData({templateData:datas});
    }
    else{
      datas[idx].pre = datas[idx].preTmp;
      this.setData({templateData:datas});
    }
  },
  onAreaConfirm(e){
    console.log(e);
    var obj = this.data.templateData[2];
    if(e.type == "confirm"){
      const arr = e.detail.values;
      var str = "";
      for(var i=0; i<arr.length; i++){
        str += arr[i].name;
        if(i < arr.length - 1){
          str += "，";
        }
      }
      obj.pre = str;
      obj.txt = str;
      obj.inputStyle = "color:black;";
    }
    else{
      obj.pre = "请输入所在地";
      obj.txt = "";
      obj.inputStyle = "";
    }
    this.setData({popArea:false,templateData:this.data.templateData});
  },
  onMaskClick(e){
    this.setData({popArea:false});
  },
  onAddressClick(e){
    //console.log(e);
    const isPop = e.currentTarget.dataset.ispop;
    if(isPop){
      this.setData({popArea:!this.data.popArea});
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
    console.log("address page hide!");
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    const app = getApp();
    const _this = this;
    const curData = {
      type:"setAddress",
      cmdType:"set",
      openid:app.globalData.openid,
      list:_this.data.addressList
    }
    //保存地址
    wx.cloud.callFunction({
      name: 'quickstartFunctions',
      data: curData,
      complete: res => {
        console.log('setAddress set: ', res);
      }
    })
    console.log("address page onUnload!",this.data.addressList);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})